package com.ziyun.erp.modules.quotePrintFactory.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ziyun.erp.common.e.quote.QuotePrintFactoryEnum;
import com.ziyun.erp.common.e.quote.QuotePrintFactoryPriceColourEnum;
import com.ziyun.erp.common.e.quote.QuotoPrintPrintTypeEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quotePrint.entity.QuotePrintEntity;
import com.ziyun.erp.modules.quotePrint.service.QuotePrintService;
import com.ziyun.erp.modules.quotePrintFactory.dao.QuotePrintFactoryDao;
import com.ziyun.erp.modules.quotePrintFactory.entity.QuotePrintFactoryEntity;
import com.ziyun.erp.modules.quotePrintFactory.service.QuotePrintFactoryService;
import com.ziyun.erp.modules.quotePrintFactory.vo.QuotePrintFactoryEntityVo;
import com.ziyun.erp.modules.quotePrintFactoryPrice.entity.QuotePrintFactoryPriceEntity;
import com.ziyun.erp.modules.quotePrintFactoryPrice.service.QuotePrintFactoryPriceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;


@Service("quotePrintFactoryService")
public class QuotePrintFactoryServiceImpl extends ServiceImpl<QuotePrintFactoryDao, QuotePrintFactoryEntity> implements QuotePrintFactoryService {
    @Autowired
    private QuotePrintService quotePrintService;

    @Autowired
    private QuotePrintFactoryPriceService quotePrintFactoryPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<QuotePrintFactoryEntity> page = new Query<QuotePrintFactoryEntity>(params).getPage();
        page.setRecords(this.baseMapper.selectQuotePrintFactoryList(page, params));
        return new PageUtils(page);
    }

    //获取工厂印刷机配置
    @Override
    public Map<String, Object> listQuotePrintFactory(Map<String, Object> params) {

        Map<String, Object> rsData = new HashMap<>();

        PageUtils page = this.queryPage(params);
        //查询所有的打印机配置
        List<QuotePrintEntity> quotePrintList = quotePrintService.selectList(new EntityWrapper<QuotePrintEntity>());

        Map<String, List> map = new HashMap<>();
        //存放数据到userdata
        map.put("quotePrintList", quotePrintList);
        //存放打印机打印类型
        map.put("quotePrintPrintTypeEnum", JsonUtils.enumToList(QuotoPrintPrintTypeEnum.class));
        //存放工厂打印机机器类型
        map.put("quotePrintFactoryEnum", JsonUtils.enumToList(QuotePrintFactoryEnum.class));
        //存放打印机颜色
        map.put("quotePrintFactoryPriceColourEnum", JsonUtils.enumToList(QuotePrintFactoryPriceColourEnum.class));

        rsData.put("page", page);
        rsData.put("userData", map);

        return rsData;
    }

    /**
     * 获取印刷机配置详情
     *
     * @param id
     * @return
     */
    @Override
    public QuotePrintFactoryEntityVo getInfo(Integer id) {
        //先创建一个包装类用于接受最终结果
        QuotePrintFactoryEntityVo quotePrintFactoryVo = new QuotePrintFactoryEntityVo();
        //先查询quotePrintFactory
        QuotePrintFactoryEntity quotePrintFactory = this.selectById(id);
        //根据quotePrintFactory的print_id从quotePrint查询printName
        QuotePrintEntity quotePrintEntity = quotePrintService.selectById(quotePrintFactory.getPrintId());
        quotePrintFactory.setPrintName(quotePrintEntity.getPrintType());
        //再查询quote_print_factory_price,先得到不是特种纸张的价格
        List<QuotePrintFactoryPriceEntity> quotePrintFacotoryPirceList = quotePrintFactoryPriceService.selectList(new EntityWrapper<QuotePrintFactoryPriceEntity>().eq("print_factory_id", id)
                        .eq("del_flag",0).eq("can_uv",0));
        //查询特种纸张价格
        List<QuotePrintFactoryPriceEntity> uvQuotePrintFacotoryPirceList = quotePrintFactoryPriceService.selectList(new EntityWrapper<QuotePrintFactoryPriceEntity>().eq("print_factory_id", id)
                .eq("del_flag",0).eq("can_uv",1));

        //封装数据到包装类中
        quotePrintFactoryVo.setuVQuotePrintFacotoryPirceList(uvQuotePrintFacotoryPirceList);
        quotePrintFactoryVo.setQuotePrintFacotoryPirceList(quotePrintFacotoryPirceList);
        quotePrintFactoryVo.setQuotePrintFactory(quotePrintFactory);
        return quotePrintFactoryVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQuotePrintFactory(Integer[] ids, Map<String, Object> map) {
        this.deleteUpdateBatchIds(ids, map);
        //伪删除对应的quote_print_factory_price
        this.quotePrintFactoryPriceService.deleteUpdateBatchId(ids, map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDisplay(Integer id) {
        QuotePrintFactoryEntity quotePrintFactoryEntity = this.selectById(id);
        boolean display = !quotePrintFactoryEntity.getDisplay();
        quotePrintFactoryEntity.setDisplay(display);
        this.updateById(quotePrintFactoryEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R save(QuotePrintFactoryEntityVo quotePrintFactoryVo,String createName) {
        //先获取QuotePrintFactoryEntity进行插入，并返回Id
        QuotePrintFactoryEntity quotePrintFactory = quotePrintFactoryVo.getQuotePrintFactory();
        //判断是否有重复的颜色
        if(!isRepeat(quotePrintFactoryVo.getQuotePrintFacotoryPirceList())){
            return R.error("机器价格设置中存在相同颜色！请检查参数后再点保存!");
        }
        if(!isRepeat(quotePrintFactoryVo.getuVQuotePrintFacotoryPirceList())){
            return R.error("特种纸价格设置中存在相同颜色！请检查参数后再点保存!");
        }
        if (quotePrintFactory != null) {
            //根据所选打印机去查询
            QuotePrintEntity quotePrintEntity = quotePrintService.selectOne(new EntityWrapper<QuotePrintEntity>().eq("print_type", quotePrintFactory.getPrintName()));
            //设置创建时间
            quotePrintFactory.setCreateDate(new Date());
            //设置创建人
            quotePrintFactory.setCreateUser(createName);
            //设置quotePrintFactory的print_id属性
            quotePrintFactory.setPrintId(quotePrintEntity.getId());

            List<QuotePrintFactoryPriceEntity> list=new ArrayList<QuotePrintFactoryPriceEntity>();
            //插入一条quotePrintFactory，并获取主键
            this.baseMapper.insert(quotePrintFactory);
            for (QuotePrintFactoryPriceEntity quotePrintFactoryPriceEntity : quotePrintFactoryVo.getQuotePrintFacotoryPirceList()) {
                //设置创建人
                quotePrintFactoryPriceEntity.setCreateUser(createName);
                //设置创建时间
                quotePrintFactoryPriceEntity.setCreateDate(new Date());
                //设置print_factory_id
                quotePrintFactoryPriceEntity.setPrintFactoryId(quotePrintFactory.getId());
                list.add(quotePrintFactoryPriceEntity);
            }
            for (QuotePrintFactoryPriceEntity quotePrintFactoryPriceEntity : quotePrintFactoryVo.getuVQuotePrintFacotoryPirceList()) {
                //设置创建人
                quotePrintFactoryPriceEntity.setCreateUser(createName);
                //设置创建时间
                quotePrintFactoryPriceEntity.setCreateDate(new Date());
                quotePrintFactoryPriceEntity.setCanUv(true);
                //设置print_factory_id
                quotePrintFactoryPriceEntity.setPrintFactoryId(quotePrintFactory.getId());
                list.add(quotePrintFactoryPriceEntity);
            }
            //插入数据到quote_print_factory_price表中
            quotePrintFactoryPriceService.insertBatch(list);
        }
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R update(QuotePrintFactoryEntityVo quotePrintFactoryVo) {
        //先获取QuotePrintFactoryEntity进行插入，并返回Id
        QuotePrintFactoryEntity quotePrintFactory = quotePrintFactoryVo.getQuotePrintFactory();
        //判断是否有重复的颜色
        if(!isRepeat(quotePrintFactoryVo.getQuotePrintFacotoryPirceList())){
            return R.error("机器价格设置中存在相同颜色！请检查参数后再点保存!");
        }
        if(!isRepeat(quotePrintFactoryVo.getuVQuotePrintFacotoryPirceList())){
            return R.error("特种纸价格设置中存在相同颜色！请检查参数后再点保存!");
        }
        List<QuotePrintFactoryPriceEntity> quotePrintFactoryPriceEntityList= quotePrintFactoryVo.getQuotePrintFacotoryPirceList();
        for(int i=0;i<quotePrintFactoryVo.getuVQuotePrintFacotoryPirceList().size();i++){
            QuotePrintFactoryPriceEntity quotePrintFactoryPriceEntity = quotePrintFactoryVo.getuVQuotePrintFacotoryPirceList().get(i);
            quotePrintFactoryPriceEntity.setCanUv(true);
            quotePrintFactoryPriceEntityList.add(quotePrintFactoryPriceEntity);
        }

        //根据所选打印机去查询
        QuotePrintEntity quotePrintEntity = quotePrintService.selectOne(new EntityWrapper<QuotePrintEntity>().eq("print_type", quotePrintFactory.getPrintName()));
        quotePrintFactory.setPrintId(quotePrintEntity.getId());
        if (quotePrintFactory != null) {
            //先更新打印机工厂相关数据
            this.updateById(quotePrintFactory);
        }
        //数据库中工厂印刷机价格
        List<QuotePrintFactoryPriceEntity> quotePrintFactoryPriceEntities = quotePrintFactoryPriceService.selectList(new EntityWrapper<QuotePrintFactoryPriceEntity>().eq("print_factory_id", quotePrintFactory.getId()).eq("del_flag", 0).eq("display",1));
        //处理新增的
        List<QuotePrintFactoryPriceEntity> addList=new ArrayList<QuotePrintFactoryPriceEntity>();
        //处理修改的
        List<QuotePrintFactoryPriceEntity> updateList=new ArrayList<QuotePrintFactoryPriceEntity>();
        //处理删除的
        List<QuotePrintFactoryPriceEntity> delList=new ArrayList<QuotePrintFactoryPriceEntity>();
        for(int i=0;i<quotePrintFactoryPriceEntityList.size();i++){
            QuotePrintFactoryPriceEntity quotePrintFactoryPriceEntity = quotePrintFactoryPriceEntityList.get(i);
            if(quotePrintFactoryPriceEntity.getId()==null){
                quotePrintFactoryPriceEntity.setPrintFactoryId(quotePrintFactory.getId());
                quotePrintFactoryPriceEntity.setCreateUser(quotePrintFactory.getUpdateUser());
                quotePrintFactoryPriceEntity.setUpdateDate(new Date());
                addList.add(quotePrintFactoryPriceEntity);
            }else{
                quotePrintFactoryPriceEntity.setUpdateDate(quotePrintFactory.getUpdateDate());
                quotePrintFactoryPriceEntity.setUpdateUser(quotePrintFactory.getUpdateUser());
                updateList.add(quotePrintFactoryPriceEntity);
            }
        }
        List<Integer> idList = updateList.stream().map(QuotePrintFactoryPriceEntity::getId).collect(Collectors.toList());
        for(int i=0;i<quotePrintFactoryPriceEntities.size();i++){
            if(!idList.contains(quotePrintFactoryPriceEntities.get(i).getId())){
                delList.add(quotePrintFactoryPriceEntities.get(i));
            }
        }
        for(int i=0;i<delList.size();i++){
          QuotePrintFactoryPriceEntity  quotePrintFactoryPriceEntity=delList.get(i);
            quotePrintFactoryPriceEntity.setDelFlag(true);
            quotePrintFactoryPriceEntity.setUpdateUser(quotePrintFactory.getUpdateUser());
            quotePrintFactoryPriceEntity.setUpdateDate(quotePrintFactory.getUpdateDate());
        }
        if(delList.size()>0){
            quotePrintFactoryPriceService.updateBatchById(delList);
        }
        if(updateList.size()>0){
            quotePrintFactoryPriceService.updateBatchById(updateList);
        }
        if(addList.size()>0){
            //插入数据到quote_print_factory_price表中
            quotePrintFactoryPriceService.insertBatch(addList);
        }
      return R.ok();

    }

    @Override
    public void deleteUpdateBatchIds(Integer[] ids, Map<String, Object> map) {
        this.baseMapper.deleteUpdateBatchIds(ids, map);
    }

    //检查颜色是否有重复,重复返回false,否则返回true
    public Boolean isRepeat(List<QuotePrintFactoryPriceEntity> quotePrintFactoryPriceEntities){
        Set<String> set=new HashSet<>();
        for (int i=0;i<quotePrintFactoryPriceEntities.size();i++){
            set.add(quotePrintFactoryPriceEntities.get(i).getColour());
        }
        return  set.size()==quotePrintFactoryPriceEntities.size();
    }
}
